package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.StemealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Dishservice;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishserviceImpl implements Dishservice {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    StemealDishMapper stemealDishMapper;

    @Override
    public PageResult page(DishPageQueryDTO dishDTO) {
        PageHelper.startPage(dishDTO.getPage(), dishDTO.getPageSize());
        Page page = (Page) dishMapper.page(dishDTO);
        long total = page.getTotal();
        List<Dish> dishList = page.getResult();
        List<DishVO> dishVOList = new ArrayList<>();
        //将 List<Dish> 转换成List<DishVO>
        for (Dish dish : dishList) {
            DishVO dishVO = new DishVO();
            //拷贝
            BeanUtils.copyProperties(dish, dishVO);
            //查询当前菜的分类名称 封装到dishVO
            String categoryName = categoryMapper.selectNameId(dish.getCategoryId());
            dishVO.setCategoryName(categoryName);
            dishVOList.add(dishVO);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(dishVOList);
        return pageResult;
    }

    @Override
    @Transactional
    public void sevetAndFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setCreateUser(BaseContext.getCurrentId());
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    @Override
    public void removeBatch(List<Long> ids) {
        Integer startCount = dishMapper.countStart(ids);
        if (startCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        Integer dishCount = stemealDishMapper.countDish(ids);
        if (dishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品数据
        dishMapper.deleteBatch(ids);
        //删除菜品对应的口味数据
        dishFlavorsMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO queryById(Long id) {
        Dish dish = dishMapper.selectById(id);
        List<DishFlavor> dishFlavors = dishFlavorsMapper.selectByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        //修改口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            dishFlavorsMapper.deleteByDishIds(Arrays.asList(dishDTO.getId()));
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorsMapper.insertBatch(flavors);
        }else {
            dishFlavorsMapper.deleteByDishIds(Arrays.asList(dishDTO.getId()));
        }
    }
    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.update(dish);
    }

    @Override
    public List<DishItemVO> listByCategoryId1(Long id) {
        List<DishItemVO> dishsById = stemealDishMapper.getDishsById(id);
        return dishsById;
    }

    @Override
    public List<DishVO> listByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.listByCategoryId(categoryId);
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            //查询菜的口味
            List<DishFlavor> dishFlavors = dishFlavorsMapper.selectByDishId(dish.getId());
            dishVO.setFlavors(dishFlavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    }
